/**
 * File: FastqWriterUtil.java
 * Created by: mhaimel
 * Created on: 2 Apr 2009
 * CVS:  $Id: FastqWriterUtil.java,v 1.1 2009/04/22 07:38:10 mhaimel Exp $
 */
package uk.ac.ebi.curtain.utils.file.impl;

import java.io.PrintWriter;

import org.apache.commons.lang.StringUtils;

import uk.ac.ebi.curtain.utils.file.EntryMapperBean;

/**
 * @author mhaimel
 *
 */
public class FastqWriterUtil extends AbstractWriter {

	private static final String defaultQuality = "c";
	private static final char space = ' ';
	private static final char entryStart = '@';
	private static final char annotStart = '+';

	@Override
	public void write(PrintWriter out, EntryMapperBean bean){
		
		// ID 
		printId(out, bean);
		
		// Sequence
		printSequence(out, bean);
		
		// Annotation
		printAnnotation(out, bean);
		
		// Qualifier
		printQualifier(out,bean);

	}

	private void printQualifier(PrintWriter out, EntryMapperBean bean) {
		String qual = bean.getQuality();
		if(StringUtils.isBlank(qual)){
			// generate Quality line
			qual = StringUtils.repeat(defaultQuality, bean.getSequence().length());
		}
		out.print(qual);
		out.println();
	}

	private void printAnnotation(PrintWriter out, EntryMapperBean bean) {
		out.print(annotStart);
		if(StringUtils.isNotBlank(bean.getAnnotations())){
			out.print(bean.getAnnotations());
		}
		out.println();
	}

	private void printSequence(PrintWriter out, EntryMapperBean bean) {
		out.println(bean.getSequence());
	}

	private void printId(PrintWriter out, EntryMapperBean bean) {
		out.print(entryStart);
		out.print(bean.getId());
		if(StringUtils.isNotBlank(bean.getHeader())){
			out.print(space);
			out.print(bean.getHeader());
		}
		out.println();
	}
	
}
